<?php

namespace App\Http\Controllers\Notice;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Notice\NoticeTemplate;
use App\Helpers\Code;
use Illuminate\Support\Facades\Validator;

class NoticeTemplateController extends Controller
{

    public function __construct()
    {
        $this->_validations();
        $this->model = new NoticeTemplate();
    }

    /**
     * 创建sms模板 表单提交 post请求 操作表notice_templates
     * @param title content description  type
     * @param template_id 第三方模板id
     */
    public function addTemplate(Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }
            $this->result['data'] = $this->model->addTemplate($data);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 删除短信模板 delete请求 操作表notice_template
     * @param id  notice_template表中id
     */
    public function deleteTemplate($id)
    {
        try {
            $noticeTemplate = $this->model->deleteTemplate($id);
            $this->checked($noticeTemplate, ['null' => 'common.delete_failure']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑短信模板 操作表notice_template
     * @param id  notice_template表中id
     */
    public function getTemplate($id)
    {
        try {
            $template = $this->model->getTemplate($id);
            $this->checked($template, ['null' => 'common.edit_failure']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function editTemplate(Request $request, $id)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['data_scope']));
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }
            $template = $this->model->editTemplate($id, $data);
            $this->checked($template, ['null' => 'common.edit_failure']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取模板列表 加分页 get请求
     */
    public function getTemplateList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);
            $limit = (int) $request->input('limit');
            $sorter = $this->handleSort($request, $this->model->sorters);
            return preprocess($this->model->getTemplateList($limit, $sorter, $search));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * sms群发 进入群发模板页面 获取全部模板 get请求
     */
    public function getTemplateListAll()
    {
        try {
            $this->result['data'] = $this->model->getTemplateListAll();
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validations()
    {
        $this->rules = [
            'title' => 'required|max:50',
            'content' => 'required',
            'template_id' => 'required',
            'description' => 'required',
            'type' => 'required',
        ];

        $this->messages = [
            'title.required' => trans('notice.title_required'),
            'title.max' => trans('notice.title_max'),
            'content.required' => trans('notice.content_required'),
            'template_id.required' => trans('notice.template_id_required'),
            'description.required' => trans('notice.description_required'),
            'type.required' => trans('notice.type_required'),
        ];
    }

}
